package GUI.Relatorio;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import Facade.Facade;
import Objetos.Usuario;
import com.toedter.calendar.JDateChooser;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;

/**
 *
 * @author celio
 */
public class Gastos extends JPanel {

    Calendar data;
    JTable tabela;
    JLabel numServicos,totalValor,totalGasto,percEncimaTotal, tiketMedio,gastoMedio, inicio, fim;
    JScrollPane scroller;
    JDateChooser dataIni, dataFim;
    JButton ok;
    Facade fa;
    
    public Gastos(final Usuario usu) {

        JLabel tela = new JLabel("... :::   Cálculo de gastos com O.S.   ::: ...");
        tela.setBounds(400, 10, 500, 20);
        add(tela);

        data = Calendar.getInstance();
        dataIni = new JDateChooser();
        dataFim = new JDateChooser();
        
        ok = new JButton("Consultar");
        
        inicio = new JLabel("Início");
        fim = new JLabel("Fim");
        numServicos = new JLabel();
        totalValor = new JLabel();
        totalGasto = new JLabel();
        percEncimaTotal = new JLabel();
        tiketMedio = new JLabel();
        gastoMedio = new JLabel();
        
        inicio.setBounds(20, 30, 100, 20);
        dataIni.setBounds(10, 50, 100, 20);
        fim.setBounds(130, 30, 100, 20);
        dataFim.setBounds(120, 50, 100, 20);
        ok.setBounds(230, 50, 100, 20);
        
        numServicos.setBounds(10, 110, 200, 20);
        totalValor.setBounds(10, 140, 200, 20);
        totalGasto.setBounds(10, 170, 200, 20);
        percEncimaTotal.setBounds(10, 200, 200, 20);
        tiketMedio.setBounds(10, 230, 200, 20);
        gastoMedio.setBounds(10, 260, 200, 20);
        
        add(inicio);
        add(dataIni);
        add(fim);
        add(dataFim);
        add(ok);
        add(numServicos);
        add(totalValor);
        add(totalGasto);
        add(percEncimaTotal);
        add(tiketMedio);
        add(gastoMedio);
        
        totalValor.setForeground(Color.blue);
        totalGasto.setForeground(Color.red);
        
        ok.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            
                try {
            remove(scroller);
            numServicos.setText("");
            totalValor.setText("");
            totalGasto.setText("");
            percEncimaTotal.setText("");
            tiketMedio.setText("");
            gastoMedio.setText("");
                

        } catch (NullPointerException ex) {
        }
        fa = new Facade();
        tabela = fa.calcularGastosPorPeríodo_OS(dataIni.getDate().toLocaleString(),dataFim.getDate().toLocaleString(), usu);
        tabela.setFont(new Font("Times New Roman", Font.BOLD, 14));
        scroller = new JScrollPane(tabela, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroller.setBounds(350, 50, 720, 400);
        add(scroller);
        if(tabela==null){
            
            JOptionPane.showMessageDialog(null, "Sem O.S. no período selecionado!!!");
        }else{
                try {
                   
                    numServicos.setText("Número de O.S.: "+tabela.getRowCount());
                    totalValor.setText("Somatório dos valors: "+Funcoes.funcoes.calculaTotal(tabela,4,"+"));
                    totalGasto.setText("Somatório dos gastos: "+Funcoes.funcoes.calculaTotal(tabela,5,"+"));
                    double totalValor = Funcoes.funcoes.calculaTotalMedia(tabela, 4, "+",1);
                    double totalGasto = Funcoes.funcoes.calculaTotalMedia(tabela, 5, "+",1);
                    double perc = totalGasto*100/totalValor;
                    percEncimaTotal.setText("Valor x Gasto: "+Funcoes.funcoes.paraFormatoDinheiro(perc)+"%");
                    tiketMedio.setText("Tiket médio: "+Funcoes.funcoes.paraFormatoDinheiroRelatorio(Funcoes.funcoes.calculaTotalMedia(tabela, 4, "+",tabela.getRowCount())));
                    gastoMedio.setText("Gasto médio: "+Funcoes.funcoes.paraFormatoDinheiroRelatorio(Funcoes.funcoes.calculaTotalMedia(tabela, 5, "+",tabela.getRowCount())));
                
                } catch (ParseException|NullPointerException ex) {
                    Logger.getLogger(Gastos.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
            }
        });
        
        setLayout(null);
        setBackground(Color.LIGHT_GRAY);
        setBorder(BorderFactory.createBevelBorder(1, Color.DARK_GRAY, Color.DARK_GRAY));
        setSize(800, 800);
        setVisible(true);
        revalidate();
        repaint();
    }
    
    

}
